package com.lijy.awt;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author Lijingyu on 2017/10/19.
 */
public class ChoiceNew extends Applet {

    String[] description = { "Ebullient", "Obtuse",
            "Recalcitrant", "Brilliant", "Somnescent",
            "Timorous", "Florid", "Putrescent" };

    TextField t = new TextField(100);
    Choice c = new Choice();
    Button b = new Button("Add items");
    int count = 0;

    public void init() {
        t.setEditable(false);
        add(t);
        for (int i = 0; i < 4; i++) {
            c.addItem(description[count++]);
        }
        add(c);
        add(b);
        c.addItemListener(new CL());
        b.addActionListener(new BL());


    }

    public static void main(String[] args) {
        ChoiceNew applet = new ChoiceNew();
        Frame aFrame = new Frame("ChoiceNew");
        aFrame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
        aFrame.add(applet, BorderLayout.CENTER);
        aFrame.setSize(750,100);
        applet.init();
        applet.start();
        aFrame.setVisible(true);
    }

    private class CL implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            t.setText("index: " +  c.getSelectedIndex() + "   " + e.toString());
        }
    }

    private class BL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if(count < description.length) {
                c.addItem(description[count++]);
            }
        }
    }

}
